<template>
	<div>
		<el-dialog title="编辑" v-model="isShowDialog" width="600px" @close="closeDialog">
			<el-form ref="formRef" :model="formModel" :rules="formRules" size="default" label-width="120px">
				<el-form-item label="源平台ID" prop="sourcePlatformId">
					<el-input v-model="formModel.sourcePlatformId" show-word-limit maxlength="100" placeholder=""
						clearable></el-input>
				</el-form-item>
				<el-form-item label="路径" prop="path">
					<el-input v-model="formModel.path" show-word-limit maxlength="50" placeholder="" clearable></el-input>
				</el-form-item>
				<el-form-item label="目标平台ID" prop="targetPlatformId">
					<el-input v-model="formModel.targetPlatformId" show-word-limit maxlength="100" placeholder=""
						clearable></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="validFlag">
					<el-select v-model="formModel.validFlag" placeholder="">
						<el-option v-for="item in yesOrNoEnums" :key="item.key" :label="item.label" :value="item.key" />
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => closeDialog()" size="default">取 消</el-button>
					<el-button type="primary" :loading="submitLoading" @click="onSubmit" size="default">提 交</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, ref, inject } from 'vue';
import type { FormInstance } from 'element-plus';
import { PermissionInfoEditParamModel, PermissionInfoDetailModel } from '/@/interfaces/JdfsystemModel';
import { handleEdit, handleValidateForm } from '../permission-info-wrap';
import useObjectUtil from '/@/utils/objectUtil';
import { DictInfo } from '/@/enums/dict-info-enum';

interface State {
	isShowDialog: boolean | true,
	submitLoading: boolean | false,
	formModel: PermissionInfoEditParamModel,
	formRules: any,
	yesOrNoEnums: any[],
}
export default defineComponent({
	name: 'PermissionInfoEdit',
	components: {},
	setup(props, context) {
		const state = reactive<State>({
			isShowDialog: true,
			submitLoading: false,
			formModel: {
				id: undefined,
				sourcePlatformId: undefined,
				path: undefined,
				targetPlatformId: undefined,
				validFlag: 1,
			},
			formRules: {
				id: [{ required: true, message: 'ID不能为空', trigger: 'change' }],
				sourcePlatformId: [{ required: true, message: '源平台不能为空', trigger: 'change' }],
				path: [{ required: true, message: '路径不能为空', trigger: 'change' }],
				targetPlatformId: [{ required: true, message: '目标平台不能为空', trigger: 'change' }],
				validFlag: [{ required: true, message: '状态', trigger: 'change' }]
			},
			yesOrNoEnums: DictInfo.yesOrNo,
		});
		const methodMap = reactive({
			// 关闭弹窗
			closeDialog: function () {
				state.isShowDialog = false;
				context.emit('onClose');
			},
			// 提交
			onSubmit: async function () {
				state.submitLoading = true;
				setTimeout(() => state.submitLoading = false, 3000);
				// 表单验证
				let validate = await handleValidateForm(formRef.value);
				if (!validate) {
					state.submitLoading = false;
					return;
				}
				// 提交
				await handleEdit(state.formModel, methodMap.closeDialog);
			},
			onYesOrNoChange: function (value: number) {
				state.formModel.validFlag = value;
			},
		});
		// 表单赋值
		const detailData: PermissionInfoDetailModel | undefined = inject<PermissionInfoDetailModel>('detailData');
		if (undefined !== detailData) {
			useObjectUtil.copyProperties(detailData, state.formModel);
		} else {
			console.error("found undefined for detail");
		}

		const formRef = ref<FormInstance>();
		const validFlagRef = ref();
		return {
			validFlagRef,
			formRef,
			...toRefs(state),
			...toRefs(methodMap),
		};
	},
});
</script>
